package com.ruoyi.yuwen.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.newAdd.returnResult.Data;
import com.ruoyi.common.newAdd.returnResult.Res;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yuwen.domain.UserGoods;
import com.ruoyi.yuwen.service.IUserGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户商品库存Controller
 * 
 * @author ruoyi
 * @date 2023-03-19
 */
@RestController
@RequestMapping("/yuwen/userGoods")
public class UserGoodsController extends BaseController
{
    @Autowired
    private IUserGoodsService userGoodsService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询用户商品库存列表
     */
    @PreAuthorize("@ss.hasPermi('yuwen:userGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserGoods userGoods)
    {
        startPage();
        List<UserGoods> list = userGoodsService.selectUserGoodsList(userGoods);
        return getDataTable(list);
    }

    /**
     * 导出用户商品库存列表
     */
    @PreAuthorize("@ss.hasPermi('yuwen:userGoods:export')")
    @Log(title = "用户商品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserGoods userGoods)
    {
        List<UserGoods> list = userGoodsService.selectUserGoodsList(userGoods);
        ExcelUtil<UserGoods> util = new ExcelUtil<UserGoods>(UserGoods.class);
        util.exportExcel(response, list, "用户商品库存数据");
    }

    /**
     * 获取用户商品库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('yuwen:userGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userGoodsService.selectUserGoodsById(id));
    }

    /**
     * 新增用户商品库存
     */
    @PreAuthorize("@ss.hasPermi('yuwen:userGoods:add')")
    @Log(title = "用户商品库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserGoods userGoods)
    {
        return toAjax(userGoodsService.insertUserGoods(userGoods));
    }

    /**
     * 修改用户商品库存
     */
    @PreAuthorize("@ss.hasPermi('yuwen:userGoods:edit')")
    @Log(title = "用户商品库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserGoods userGoods)
    {
        return toAjax(userGoodsService.updateUserGoods(userGoods));
    }

    /**
     * 删除用户商品库存
     */
    @PreAuthorize("@ss.hasPermi('yuwen:userGoods:remove')")
    @Log(title = "用户商品库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userGoodsService.deleteUserGoodsByIds(ids));
    }

    /**
     * 查询用户商品库存列表（只查询用户本身）
     * @return
     */
    @GetMapping("/frond/userGoodsList")
    public Res userGoodsList()
    {
        Long userId = tokenService.getUserId();
        UserGoods userGoods = new UserGoods();
        userGoods.setUserId(userId);
        startPage();
        List<UserGoods> list = userGoodsService.selectUserGoodsList(userGoods);
        return Res.ok().put("data", Data.ok().put("total",new PageInfo(list).getTotal()).put("list",list));
    }
}
